package main;

import java.awt.*;

import static main.Game.ACCELERATION;
import static main.Game.RIDE;
import static main.Register.HORSES;
import static main.Register.ZOMBIE;


public class Player extends LivingEntity {

    public Player(Image left,Image stepLeft, Image right, Image stepRight, Image stay) {
        super(left, stepLeft, right, stepRight, stay, 0, 0);
        boxWidth = 16;
        boxHeight = 32;
    }
    public boolean playerRide = false;
    public LivingEntity playerRideOn = null;

    @Override
    public void update() {
        super.update();

        if (locateIn(33,ZOMBIE)) {
            health -= 1;

        }
        if (RIDE && COOLDOWN == 0) {
            COOLDOWN = 10;
            if (playerRideOn == null) {
                for (int i = 0; i < HORSES.size(); i++) {
                    if (locateIn(40, HORSES.get(i))) {
                        playerRideOn = HORSES.get(i);
                        playerRide = true;

                        boxWidth = playerRideOn.boxWidth + 4;
                        //boxWidthPlus = -6;
                        boxHeight = 46;
                        boxHeightPlus = 6;

                        health = playerRideOn.health;

                        HORSES.remove(i);
                        break;

                    }
                }

            } else {
                boxWidth = 16;
                boxHeight = 32;
                boxHeightPlus = 0;
                boxWidthPlus = 0;


                playerRideOn.health = health;
                playerRideOn.setPos((int) posX, (int) (posY + height / 2 + playerRideOn.height / 2 + 2));
                HORSES.add((Horse) playerRideOn);
                playerRideOn = null;
                playerRide = false;
            }
        }


        toPosX = posX;
        toPosY = posY;
        double stepx = 0;
        double stepy = 0;

        if (Game.VK_LEFT || Game.VK_A) {
            stepx--;
        }
        if (Game.VK_RIGHT || Game.VK_D) {
            stepx++;
        }
        if (Game.VK_UP || Game.VK_W) {
            stepy--;
        }
        if (Game.VK_DOWN || Game.VK_S) {
            stepy++;
        }

        turn = stepx == 0 ? Integer.compare((int) stepy, 0) : Integer.compare((int) stepx, 0);

        stepx *= ACCELERATION ? 2 : 1;
        stepy *= ACCELERATION ? 2 : 1;
        stepx *= playerRide ? 2 : 1;
        stepy *= playerRide ? 2 : 1;

        if(moveIsTrue(stepx * 2, stepy * 2)) {
            posX += stepx * 2;
            posY += stepy * 2;
        } else if (moveIsTrue(stepx, stepy)) {
            posX += stepx;
            posY += stepy;
        }
    }


}
